package com.vcs.frm.web.school;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vcs.frm.domain.City;
import com.vcs.frm.domain.State;
import com.vcs.frm.service.CommonService;
import com.vcs.frm.web.form.school.NewAdmissionForm;

@Controller
@RequestMapping("/school/newAdmissionForm")
public class NewAdmissionController {

	@Autowired
	private CommonService commonService;

	Log log = LogFactory.getLog(NewAdmissionController.class);
	List<String> sexList;
	List<City> cityList;
	List<State> stateList;
	List<String> countryList;

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public NewAdmissionController() {

	}

	/**
	 * Initialize form here before rendering
	 * 
	 */

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView showNewAdmissionForm(Model model) {
		System.out.println("newAdmissionForm");
		initFormData();

		NewAdmissionForm admissionForm = new NewAdmissionForm();

		ArrayList<String> sexList = new ArrayList<String>();
		sexList.add("Male");
		sexList.add("Female");
		ModelAndView mav = new ModelAndView();
		mav.addObject("sexList", sexList);
		mav.addObject("cityList", cityList);
		mav.addObject("stateList", stateList);
		mav.addObject("countryList", countryList);
		mav.setViewName("newAdmissionForm");
		model.addAttribute("admissionForm", admissionForm);
		return mav;
	}

	/**
	 * 
	 * @param admissionForm
	 * @param bindingResult
	 * @param model
	 * @return ModelAndView map
	 */
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView processNewAdmissionForm(
			@ModelAttribute("admissionForm") @Valid NewAdmissionForm admissionForm,
			BindingResult bindingResult, Model model, HttpSession session) {

		initFormData();
		System.out.println("session : "+ session);
		ModelAndView mav = new ModelAndView();

		model.addAttribute("admissionForm", admissionForm);
		log.info(admissionForm);

		if (bindingResult.hasErrors()) {
			log.info("-------------------------111");
			log.info("SchoolController - processNewAdmissionForm - has errors");
			log.info(bindingResult);
			log.info(bindingResult.getAllErrors());
			for (ObjectError e : bindingResult.getAllErrors()) {
				log.info(e.getObjectName());
				log.info(e.getArguments());
			}
			log.info("-------------------------222");

			mav.addObject("sexList", sexList);
			mav.addObject("cityList", cityList);
			mav.addObject("stateList", stateList);
			mav.addObject("countryList", countryList);

			mav.setViewName("newAdmissionForm");
			model.addAttribute("admissionForm", admissionForm);
			return mav;
		}

		log.info("processing new admission form");
		mav.setViewName("home");
		return mav;
	}

	@RequestMapping(value = "cities/{stateId}", method = RequestMethod.GET)
	private ModelAndView getCitiesByState(@PathVariable int stateId) {
		log.info("stateId : " + stateId);
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		List<City> lst = commonService.getCitiesByState(stateId);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("ajax-resp");
		mav.addObject("cities", lst);
		return mav;
	}

	private void initFormData() {
		sexList = new ArrayList<String>();
		sexList.add("Male");
		sexList.add("Female");

		cityList = new ArrayList<City>();
		cityList.add(new City(1, "Nagpur"));
		cityList.add(new City(2, "Delhi"));
		cityList.add(new City(3, "Mumbai"));

		stateList = new ArrayList<State>();
		stateList = commonService.getAllStates();

		countryList = new ArrayList<String>();
		countryList.add("India");
	}

}
